package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.RechargedRespones;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 11:49
 */
public class RechargedBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(RechargedBizSlot.class);
    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        Map<String, Object> stdCcrRechargeMap = new HashMap<>();
        Map<String, Object> rechargeMap = new HashMap<>();
        String reqSerial = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Payment_Serial_Nbr");
        rechargeMap.put("reqSerial", reqSerial);
        String chargeSource = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Payment_Amount");
        rechargeMap.put("chargeSource", chargeSource);
        String destinationId = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Destination_Id");
        rechargeMap.put("destinationId", destinationId);
        String destinationIdType = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Destination_Id_Type");
        rechargeMap.put("destinationIdType", destinationIdType);
        String destinationAttr = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/DestinationAttr");
        rechargeMap.put("destinationAttr", destinationAttr);
        String balanceItemTypeID = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Balance_Type");
        rechargeMap.put("balanceItemTypeID", balanceItemTypeID);
        rechargeMap.put("unitTypeID", 0);
        String feeAmount = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Payment_Amount");
        rechargeMap.put("feeAmount", feeAmount);
        String BankCode = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Bank_code");
        rechargeMap.put("BankCode", BankCode);
        String PaymentMethod = HttpClientUtils.getXmlValue(xmlMessage,"/Service_Information/Payment_Method");
        rechargeMap.put("PaymentMethod", PaymentMethod);
        stdCcrRechargeMap.put("recharge",rechargeMap);
        msgBody.put("stdCcrRecharge",stdCcrRechargeMap);




        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

      			LOG.info("request body is {}", msgBodyStr.toString());
            LOG.info("webserviceUrl is {}", url);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
            LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        RechargedRespones rechargedRespones = new Gson().fromJson(respStr, RechargedRespones.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        respDataXml.put("Result", rechargedRespones.getStdCcaRecharge().getErrorCode());
        respDataXml.put("Bill_Serial_Nbr", rechargedRespones.getStdCcaRecharge().getBillSerialNbr());
        respDataXml.put("Paid_Time",new Date());
        respDataXml.put("Payment_Serial_Nbr",reqSerial);
        respDataXml.put("Latn_Id","");


        String respXmlStr="";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);

        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }

    public static void main(String[] args) {
        RechargedBizSlot rechargedBizSlot = new RechargedBizSlot();


        rechargedBizSlot.doBiz(null,null);

    }
}
